'''
使用python—requests 包进行接口测试
接口测试的原理：构造不同的参数，调用接口发送请求，对响应的结果进行断言
'''

import requests

base_url = "http://192.168.150.84:8088/futureloan/mvc/api/"

'''
发送get请求，不带参数
'''
url = base_url + "member/list"  # 获取用户列表的接口

r = requests.get(url)

print(r.text)

r_json = r.json()

# 断言
# r_json['msg'] 实际的结果，接口返回的响应信息
assert r_json['msg'] == '获取用户列表成功'

assert r_json['code'] == '10001'

assert r_json['status'] == 1

# get 请求带参数
# 1、拼接到URL后面，多个参数用&连接

url = base_url + 'member/register?mobilephone=18012345678&pwd=123456'
r = requests.get(url)
print(r.json())
assert r.json()['msg'] == '手机号码已被注册'
assert r.json()['status'] == 0
assert r.json()['code'] == '20110'

url = base_url + 'member/register?mobilephone=18012345678&pwd=1234'
r = requests.get(url)
print(r.json())
assert r.json()['msg'] == '密码长度必须为6~18'
assert r.json()['status'] == 0
assert r.json()['code'] == '20108'

'''
get 请求带参数
使用params 传参
'''
url = base_url + 'member/register'
cs = {"mobilephone": 18012345678, "pwd": 1877}
r = requests.get(url, params=cs)
print(r.json())
assert r.json()['msg'] == '密码长度必须为6~18'
assert r.json()['status'] == 0
assert r.json()['code'] == '20108'

print(r.status_code)  # 响应状态码
print(r.reason)  # 响应状态原因
print(r.text)  # 文本格式
print(r.encoding)  # 编码方式
print(r.request.headers)  # 请求头
print(r.headers)  # 响应头

'''
get请求，设置请求头伪装浏览器发送请求
'''
url = base_url + 'member/register'
cs = {"mobilephone": 18012345678, "pwd": 1877}
head = {'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"}
r = requests.get(url, params=cs, headers=head)
print(r.json())
print(r.request.headers)

